package Client;

import Message.AccountAddFriendInfo;
import utils.AccountAddFriendInfoList;
import utils.AccountAddFriendInfoListOfAll;
import utils.FriendArrayList;

import java.io.*;
import java.net.Socket;
import java.sql.Timestamp;

/**
 * 该类用来被用户确认是否同意对方的好友申请并且发送给服务端
 * 初始化该类需要传入一个AccountAddFriendInfoListOfAll类，并且对里面的所有好友申请设置agree属性
 * 再组织成AccountAddFriendInfoListOfAll类发送给服务端
 * 服务端会返回一个好友列表FriendArrayList类，GUI的事件监听可通过getFriendArrayList()方法获得该用户的所有好友列表
 * */
public class ResponseFriendRequest {
    private AccountAddFriendInfoListOfAll accountAddFriendInfoListOfAll;
    private FriendArrayList friendArrayList=null;

    public ResponseFriendRequest(AccountAddFriendInfoListOfAll accountAddFriendInfoListOfAll){
        this.accountAddFriendInfoListOfAll = accountAddFriendInfoListOfAll;
    }

    public FriendArrayList getFriendArrayList() {
        return friendArrayList;
    }

    public void setFriendArrayList(FriendArrayList friendArrayList) {
        this.friendArrayList = friendArrayList;
    }

    public static void main(String[] args) {
        Timestamp time1 = Timestamp.valueOf("2019-12-04 22:03:46.558");
        Timestamp time2 = Timestamp.valueOf("2019-12-04 22:03:15.941");
        Timestamp time3 = Timestamp.valueOf("2019-12-04 11:13:34.797");
        AccountAddFriendInfo accountAddFriendInfo1 = new AccountAddFriendInfo("111111111", "123455443", "我是XXX，想加个好友", time1, true);
        AccountAddFriendInfo accountAddFriendInfo2 = new AccountAddFriendInfo("111111111", "123455443", "我是XXX，想加个好友", time2, true);
        AccountAddFriendInfo accountAddFriendInfo3 = new AccountAddFriendInfo("111111111", "123455443", "我是XXX，想加个好友", time3, true);
        AccountAddFriendInfoList accountAddFriendInfoList1 = new AccountAddFriendInfoList();
        AccountAddFriendInfoList accountAddFriendInfoList2 = new AccountAddFriendInfoList();
        AccountAddFriendInfoList accountAddFriendInfoList3 = new AccountAddFriendInfoList();
        accountAddFriendInfoList1.add(accountAddFriendInfo1);
        accountAddFriendInfoList2.add(accountAddFriendInfo2);
        accountAddFriendInfoList3.add(accountAddFriendInfo3);
        AccountAddFriendInfoListOfAll accountAddFriendInfoListOfAll = new AccountAddFriendInfoListOfAll();
        accountAddFriendInfoListOfAll.add(accountAddFriendInfoList1);
        accountAddFriendInfoListOfAll.add(accountAddFriendInfoList2);
        accountAddFriendInfoListOfAll.add(accountAddFriendInfoList3);
        //上面用来构建测试用

        ResponseFriendRequest responseFriendRequest = new ResponseFriendRequest(accountAddFriendInfoListOfAll);
        try {
            //将accountAddFriendInfoListOfAll传给服务端。
            Socket socket = new Socket(ServerInfo.SERVER_IP_ADDRESS, ServerInfo.SERVER_RESPONSEFRIENDREQUEST_PORT);
            OutputStream outputStream = socket.getOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(accountAddFriendInfoListOfAll);
            //获得服务器的回应。
            InputStream inputStream = socket.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            responseFriendRequest.setFriendArrayList((FriendArrayList) objectInputStream.readObject()); //此处获得该用户的好友列表
            FriendArrayList friendArrayList = responseFriendRequest.getFriendArrayList();
            int size=friendArrayList.size();
            for(int i=0;i<size;i++){
                System.out.println("该用户的好友"+i+": "+friendArrayList.get(i));
            }

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
